package culton.android.sms;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import culton.android.CultOnHome;
import culton.android.R;
import culton.android.data.DataHelper;
import culton.android.entity.Cinema;

public class SMSCinemasActivity extends ListActivity {

	private ProgressDialog m_ProgressDialog = null;
	private Runnable viewCinemas;
	private SMSCinemasArrayAdapter cinemaAdapter = null;	 
	ArrayList<Cinema> listCinemas = new ArrayList<Cinema>();	

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
                
        //Configuring buttons in Action Bar
        ImageView homeBtn = (ImageView) findViewById(R.id.homeButton);
        homeBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		startActivity (new Intent(SMSCinemasActivity.this, CultOnHome.class));
        	}
        });
        
        ImageView leftArrowBtn = (ImageView) findViewById(R.id.leftArrow);
        leftArrowBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		startActivity (new Intent(SMSCinemasActivity.this, CultOnHome.class));
        	}
        });

        Intent mIntent = getIntent();
        mIntent.putExtra("find", "movies");
        
        //Get tracker number from preferences
        SharedPreferences settings =  getSharedPreferences("CultOnPreferences", MODE_PRIVATE);
        String trackerNumber = settings.getString("tracker", "5556");
        mIntent.putExtra("tracker", trackerNumber);
        
        this.cinemaAdapter = new SMSCinemasArrayAdapter(this, R.layout.cinemas, listCinemas, mIntent);
        setListAdapter(this.cinemaAdapter);

        viewCinemas = new Runnable(){        
            public void run() {
            	retrieveCinemas();
            }
        };
	    
        Thread thread =  new Thread(null, viewCinemas, "MagentoBackground");
	        thread.start();
	        m_ProgressDialog = ProgressDialog.show(SMSCinemasActivity.this,    
	              "Please wait...", "Retrieving data ...", true);
    }
			    
    private void retrieveCinemas(){
    	try{
    		DataHelper dataHelper = new DataHelper(SMSCinemasActivity.this);
    		listCinemas = dataHelper.selectAll();
    		dataHelper.close();
			Thread.sleep(1000);
			
    	} catch (Exception e) {
            Log.e("BACKGROUND_PROC", e.getMessage());
    	}
    	runOnUiThread(returnRes);
    }
    
    private Runnable returnRes = new Runnable() {

        public void run() {
            if(listCinemas != null && listCinemas.size() > 0){
                cinemaAdapter.notifyDataSetChanged();
                
                for(int i = 0; i < listCinemas.size(); i++)
                	cinemaAdapter.add(listCinemas.get(i));
            }
            m_ProgressDialog.dismiss();
            cinemaAdapter.notifyDataSetChanged();
        }
    };    
}
